// app.js
App({
  onLaunch() {
    // 初始化本地存储
    this.initStorage();
  },

  // 初始化存储数据
  initStorage() {
    const records = wx.getStorageSync('learningRecords') || {
      totalTimes: 0,
      correctRate: 0,
      challengeLevel: 1,
      challengeProgress: 0
    };
    wx.setStorageSync('learningRecords', records);
  },

  // 更新学习记录
  updateRecords(type, isCorrect) {
    const records = wx.getStorageSync('learningRecords') || {
      totalTimes: 0,
      correctCount: 0,
      correctRate: 0
    };
    
    records.totalTimes = (records.totalTimes || 0) + 1;
    if (isCorrect) {
      records.correctCount = (records.correctCount || 0) + 1;
    }
    records.correctRate = records.totalTimes > 0 
      ? ((records.correctCount / records.totalTimes) * 100).toFixed(1) 
      : 0;
    
    wx.setStorageSync('learningRecords', records);

    // 更新分类统计
    const categoryStats = wx.getStorageSync('categoryStats') || {
      letters: 0,
      words: 0,
      spelling: 0,
      listening: 0,
      challenge: 0,
      chinese: 0
    };
    
    if (categoryStats[type] !== undefined) {
      categoryStats[type] = (categoryStats[type] || 0) + 1;
      wx.setStorageSync('categoryStats', categoryStats);
    }
  },

  globalData: {
    userInfo: null
  }
});

